package com.example.entity;

import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Data
public class FlightWithStatus {
    //Flight
    String departure_airport;
    String arrival_airport;
    String departure_city;
    String arrival_city;
    String departure_date;
    String departure_day;
    String departure_time;
    String arrival_date;
    String arrival_day;
    String arrival_time;
    double distance;
    double economy_price;
    double first_price;
    double business_price;
    int baggage_limit;
    String meal;
    String meal_money;
    String gate;
    //FlightStatus
    String captain;
    String assistant_captain;
    String chief_attendant;
    int flight_crew_num;
    int economy_space;
    int first_space;
    int business_space;
    String status; //计划中、登机中、已起飞、已延误、已取消、已到达
    //Plane
    int plane_id;
    String p_model;
    String p_airline;
    int plane_age;
    String WiFi;
    int capacity;
    int economy_seat;
    int first_seat;
    int business_seat;

    public String getDuration() {
        try {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date dep = format.parse(getDeparture_time());
            Date arr = format.parse(getArrival_time());
            long diff = arr.getTime() - dep.getTime();
            long minutes = diff / (60 * 1000); // 转换为分钟
            long hours = minutes / 60;
            minutes = minutes % 60;
            return hours + "h " + minutes + "m";
        } catch (Exception e) {
            return "N/A"; // 出错时返回默认值
        }
    }
    public String getFlight_id() {
        if (Objects.equals(p_airline, "南方航空公司")){
            return "CZ" + String.format("%05d", plane_id);
        }else if(Objects.equals(p_airline, "东方航空公司")){
            return "MU" + String.format("%05d", plane_id);
        } else if (Objects.equals(p_airline, "中国国际航空公司")) {
            return "CA" + String.format("%05d", plane_id);
        } else{
            return "N/A";
        }
    }

    public int getBookedSeat(){
        return capacity - (economy_space + first_space + business_space);
    }
    public String getBookedSeatPercent(){
        return String.format("%.2f", (double) getBookedSeat() / capacity * 100) + "%";
    }
    public String getCheckInPercent(){
        if (getBookedSeat() == 0){
            return "0%";
        }
        return String.format("%.2f", (double) getBookedSeat() / getBookedSeat() * 100) + "%";
    }
}
